<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="98px">
      <el-form-item label="社保发放月份" prop="salaryMonth">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.salaryMonth"
          type="month"
          value-format="yyyy-MM"
          placeholder="选择社保发放月份">
        </el-date-picker>
      </el-form-item>


      <el-form-item label="归属部门" prop="deptId">
        <treeselect style="width: 200px"
          v-model="queryParams.deptId"
          :options="deptOptions"
          placeholder="请选择归属部门" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>

        <el-button
          type="success"
          icon="el-icon-plus"
          size="small"
          @click="handleTianBao"

        >填报</el-button>

      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="submissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />


      <el-table-column label="部门名称" align="center" prop="deptId" />


      <el-table-column label="社保发放月份" align="center" prop="salaryMonth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.salaryMonth) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="填报人数" align="center" prop="pnum" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '编辑中'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:submission:edit']"
          >修改</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listSubmission, getSubmission, delSubmission, addSubmission, updateSubmission, exportSubmission } from "@/api/system/submission";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 社保确认表格数据
      submissionList: [{
        deptId : '安庆大堂部',
        salaryMonth: '2019-01',
        pnum: 20,
        status : '已确认' ,
      },{
        deptId : '安庆大堂部',
        salaryMonth: '2019-01',
        pnum: 20,
        status : '编辑中' ,
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        salaryMonth: undefined,
        deptId: undefined,
        pnum: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },

      deptOptions: undefined,

    };
  },

  created() {
    //this.getList();
    this.getTreeselect();
  },
  methods: {

    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },

    /**
     * 查询社保确认列表
     * 根据部门编号和社保月份，查询出社保总体数据
     * 社保总体数据 是社保填报页面填报之后，点提交才会生成的
     *
     * */
    getList() {
      //this.loading = true;
      listSubmission(this.queryParams).then(response => {
        this.submissionList = response.rows;
        this.total = response.total;
        this.loading = false; //加载完毕，取消loading
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        salaryMonth: undefined,
        deptId: undefined,
        pnum: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社保确认";
    },


    /**
     *   社保修改按钮，跳转到社保填报页面
     *   传参：部门编号和社保月份
     *   填报页面自动根据参数，加载该部门在本月的社保数据
     *
     * */
    handleUpdate(row) {


      this.$router.push({
        // name:'',
        path:'/employee/employee/social',
        query:{
          salaryMonth: row.salaryMonth ,
          deptId:row.deptId
        }
      })


    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有社保确认数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSubmission(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },


    //填报按钮，跳转到社保填报页面（列表没有数据，需要生成）
    //允许用户选择社保月份和部门，生产默认社保
    handleTianBao() {
      this.$router.push({
        // name:'',
        path:'/employee/employee/social',
        query:undefined
      })
    }
  }
};
</script>
